import { loadScript } from './util'
import coordtransform from './coordtransform'
import Map from './instance'
export class BdMap extends Map {
  constructor(options) {
    super()
    this.options = Object.assign({}, options, {
      domId: 'mymap',
      point: {
        lng: '120.000284',
        lat: '31.818496',
        type: 'bd'
      },
      zoom: 16,
      ak: 'aOpsbwrpo297oX0y1ZG7VPnwBETTGH5H'
    })
    this.init()
  }
  //转化坐标
  getPoint(point) {
    let p = point
    if (point.type == 'wgs84') {
      const [lng, lat] = coordtransform.wgs84tobd09(point.lng, point.lat)
      p = Object.assign({}, point, { lng, lat })
    }
    const npoint = new BMap.Point(p.lng, p.lat)
    npoint._p = p
    return npoint
  }
  //初始化
  init() {
    this.load()
      .then((res) => {
        this.initMap()
      })
      .then((res) => {
        this.bindFns()
      })
  }
  //初始化地图
  initMap() {
    const { point, domId, zoom } = this.options
    const map = new BMap.Map(domId)
    this.map = map
    const p = this.getPoint(point)
    this.locaitonByPoint(p, zoom)
  }
  //绑定功能
  bindFns() {
    this.fnMapClick()
  }
  //通过点位定位
  locaitonByPoint(point, zoom = 0, clear = 1) {
    const { map } = this
    if (clear) {
      map.clearOverlays()
    }
    const marker = this.makeMarker(point)
    map.addOverlay(marker)
    if (zoom) {
      map.centerAndZoom(point, zoom)
    } else {
      map.panTo(point)
    }
  }
  //点击地图标记
  fnMapClick() {
    const { map } = this
    map.addEventListener('click', (res) => {
      const point = res.lnglat
      this.locaitonByPoint(point)
    })
  }
  //搜索关键词
  searchKw(options) {
    const map = this.map
    const { callback } = options
    //定义搜索
    const config = {
      pageCapacity: 10, //每页显示的数量
      onSearchComplete: (result) => {
        callback(result)
      }
    }
    //创建搜索对象
    const localsearch = new BMap.LocalSearch(map, config)
    return localsearch
  }
  //逆解析 坐标->地址
  geocoder(point) {
    return new Promise((resolve) => {
      let geo = new BMap.Geocoder()
      return geo.getLocation(point, (result) => {
        resolve(result)
      })
    })
  }
  //h5定位
  h5location() {
    var geolocation = new BMap.Geolocation()
    return new Promise((resolve, reject) => {
      geolocation.getCurrentPosition(
        function (e) {
          if (geolocation.getStatus() == BMAP_STATUS_SUCCESS) {
            resolve(e)
          } else {
            reject('定位失败')
          }
        },
        {
          enableHighAccuracy: true,
          timeout: 5e3
        }
      )
    })
  }
  //逆解析 地址->坐标
  geocoderA2P(address) {
    return new Promise((resolve) => {
      let geo = new BMap.Geocoder()
      return geo.getPoint(address, (result) => {
        resolve(result)
      })
    })
  }
  //制作marker点
  makeMarker(point) {
    return new BMap.Marker(point)
  }
  //加载地图配置
  load() {
    const baiduAk = this.options.ak
    if (!baiduAk) return Promise.reject()
    if (window.location.protocol.startsWith('https')) window.HOST_TYPE = '2' // https 下百度地图特殊记号
    window.BMap_loadScriptTime = new Date().getTime()
    return loadScript(`${window.location.protocol}//api.map.baidu.com/getscript?v=2.0&ak=${baiduAk}&services=&t=${new Date().getTime()}`)
  }
}
